function UniqueArray() {
	var self = this;
	var _data = [];
	var _uniques = [];
	var _numUniques = 0;

	self.push = function(val) {
		_data.push(val);

		if (_uniques[val[0]] === undefined || _uniques[val[0]] === 0) {
			_uniques[val[0]] = 1;
			++_numUniques;
		} else {
			++_uniques[val[0]];
		}

		if (_uniques[val[1]] === undefined || _uniques[val[1]] === 0) {
			_uniques[val[1]] = 1;
			++_numUniques;
		} else {
			++_uniques[val[1]];
		}
	}

	self.pop = function() {
		var popped = _data.pop();

		--_uniques[popped[0]];
		--_uniques[popped[1]];

		if (_uniques[popped[0]] === 0) {
			--_numUniques;
		}

		if (_uniques[popped[1]] === 0) {
			--_numUniques;
		}

		return popped;
	}

	self.getNumUniques = function() {
		return _numUniques;
	}

	self.toString = function() {
		return '(' + _data.join('),(') + ')';
	}
}



/*
 * Generate all possible edges for the given number (e) of edges.
 * e - number of edges
 * return - [[]] - length will be (e) + 1. all child arrays at a given
 * index will have a difference of the index itself.
 * e.g. generate(3) === [undefined, [[0, 1], [1, 2], [2, 3]], [[0, 2], [1, 3]], [[0, 3]]]
 */
function generate(e) {
	var x = [];

	for (var i = 1; i <= e; ++i) {
		x[i] = [];

		for (var j = 0; j <= e - i; ++j) {
			x[i].push([j, j + i]);
		}
	}

	return x;
}



/*
 * e - [[]] - returned from generate(e)
 */
function recur(e, i, j, holder, numVertices) {
	holder.push(e[i][j]);

	if (holder.getNumUniques() > numVertices) {
		holder.pop();
		return false;
	} else {
		if (i === 1) {
			return holder.toString();
		} else {
			var found = false;
			for (var k = 0; !found && k < e[i - 1].length; ++k) {
				found = recur(e, i - 1, k, holder, numVertices);
				if (found) {
					return found;
				}
			}
			holder.pop();
		}
	}
}



function driver(n, e) {
	return recur(generate(e), e, 0, new UniqueArray(), n);
}



onmessage = function(oEvent) {
	postMessage({ n: oEvent.data.n, e: oEvent.data.e, solution: driver(oEvent.data.n, oEvent.data.e) });
}
